-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
34 lines (26 loc) · 957 Bytes
/
Solution.java
File metadata and controls
34 lines (26 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Scanner;
public class Subsets {
public static void generateSubsets(char[] set, String subset, int index) {
if (index == set.length) {
System.out.println("{" + subset + "}");
return;
}
// Exclude the current element
generateSubsets(set, subset, index + 1);
// Include the current element
generateSubsets(set, subset + set[index], index + 1);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the set: ");
int n = scanner.nextInt();
char[] set = new char[n];
System.out.print("Enter the elements of the set: ");
for (int i = 0; i < n; i++) {
set[i] = scanner.next().charAt(0);
}
System.out.println("The subsets are:");
generateSubsets(set, "", 0);
scanner.close();
}
}